#include "regdef.h"
	
	.section .init
	.globl _start
	.type _start,@function

_start:
    csrwr	$r0,0x180
    csrwr	$r0,0x181
    ori	    $r12,$r0,0x09
    csrwr	$r12,0x180

    li.w    $r12,0xa0000009
    csrwr	$r12,0x181
    ori	    $r12,$r0,0x10

    ori	    $r13,$r0,0x18
    csrxchg	$r12,$r13,0x0

    lu12i.w	$r12,114688
    ori	    $r12,$r12,0x380
    csrwr	$r12,0xc

    /* Load data section */
	la.local t0, _data_lma
	la.local t1, _data
	la.local t2, _edata
	bgeu t1, t2, 2f
1:
	ld.w t3, t0, 0
	st.w t3, t1, 0
	addi.w t0, t0, 4
	addi.w t1, t1, 4
	bltu t1, t2, 1b
2:

	/* Clear bss section */
	la.local t0, __bss_start
	la.local t1, _end
	bgeu t0, t1, 2f
1:
	st.w $r0, t0, 0
	addi.w t0, t0, 4
	bltu t0, t1, 1b
2:

	ori	    $r12,$r0,0x19
    csrwr	$r12,0x180

	la.local	sp, _stack
    
	/* argc = argv = 0 */
	li.w a0, 0
	li.w a1, 0
	bl main
	/*tail exit*/

1:
	b 1b
